<?php

namespace App\Admin\Controllers;

use \App\Models\QdcsArticle;

use com_jjcbs\lib\ServiceFactory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use ext\service\ClassServiceImpl;

class ArticleController extends Controller
{
    use ModelForm;

    private $classService = null;

    public function __construct()
    {
        $this->classService = ServiceFactory::getInstance(ClassServiceImpl::class);
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('文章管理');
            $content->description('编辑');
            $content->disablePjax(true);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章管理');
            $content->description('创建');
            $content->disablePjax(true);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(QdcsArticle::class, function (Grid $grid) {

            // add filter
            $grid->filter(function(Grid\Filter $filter){
                $filter->like('title' , '标题');
               $filter->date('created_at' , '创建时间');
                $filter->equal('cls_id' , '分类')->select($this->classService->getChildOption(34));
            });
            $grid->id('ID')->sortable();
            $grid->title('标题');
            $grid->subhead('副标题');
            $grid->editer('责任编辑');
            $grid->src('来源');
            $grid->column('img', '文章主图')->display(function ($cover) {
                return "<img src='" . config('admin.upload.host') . $cover . "' width='50' height='50'>";
            });
            $grid->qdcsClass()->name('分类名');
            $grid->column('is_top' , '是否头条')->display(function($cover){
               return $cover ? '是' : '否';
            });
            $grid->column('is_hot' , '是否热门')->display(function($cover){
                return $cover ? '是' : '否';
            });
            $grid->created_at('创建时间')->sortable();
            $grid->updated_at('编辑时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(QdcsArticle::class, function (Form $form) {
            $form->display('id', '编号');
            $form->text('title' , '标题')->rules('required|min:3');
            $form->text('subhead' , '副标题')->rules('required|max:255');
            $form->text('editer' , '责任编辑')->rules('required');
            $form->text('src' , '来源')->rules('required');
            $form->image('img' , '主图');
            $form->select('cls_id','所属分类')->options($this->classService->getChildOption(34));
            $form->switch('is_top' , '设置为头条')->options([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->switch('is_hot' , '设置为热门')->options([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->text('keyword' , 'SEO：关键字(留空系统默认生成)');
            $form->text('description' , 'SEO：描述(留空系统默认生成)');
            $form->ueditor('content' , '正文');

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '编辑时间');
        });
    }
}
